package com.cn.intelligence.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator on 2017/4/17.
 */
@Controller
public class PageController {
	@Autowired
	HttpServletRequest request;

	/**
	 * 打开首页
	 */
	@RequestMapping("/")
	public String showIndex() {
		return "login";
	}

	/**
	 * 展示其他页面
	 * <p>
	 * Title: showpage
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 *
	 * @param page
	 * @return
	 */
	@RequestMapping("/{page}")
	public ModelAndView showpage(@PathVariable String page, @ModelAttribute("msg") String msg) {

		try {
			boolean falg = (boolean) request.getSession().getAttribute("login");
			String role = (String) request.getSession().getAttribute("role");
			if ("logout".equals(page)) {
				request.getSession().setAttribute("role", "");
				return new ModelAndView("redirect:/");
			}
			if (falg) {
				Map<String, String> model = new HashMap<String, String>();
				model.put("msg", msg);
				if("".equals(role)){
					return new ModelAndView("redirect:/login");
				}else{
					return new ModelAndView(page, model);
				}
				
			} else {
				return new ModelAndView("redirect:/login");
			}
		} catch (Exception e) {
			return new ModelAndView("redirect:/");
		}

	}

}
